const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const htmlWebpackPlugin = require("html-webpack-plugin");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const copyWebpackPlugin = require('copy-webpack-plugin')
const miniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  // mode: 属性默认是production
  mode: "development",
  devtool: "source-map",
  entry: "./src/index.js",
  output: {
    filename: "js/[name].[hash:6].js",
    path: path.resolve(__dirname, "./dist"),
  },
  resolve: {
    extensions: [".js", ".json", ".js", ".vue", ".css"],
    alias: {
      "@": path.resolve(__dirname, "./src"),
      vue$: 'vue/dist/vue.esm.js',
    },
  },
  devServer: {
    hot: true,

  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          miniCssExtractPlugin.loader,
          {
            loader: "css-loader",
            options: {
              importLoaders: 1,
            },
          },
          "postcss-loader",
        ],
      },
      {
        test: /\.less$/i,
        use: ["style-loader", {
          loader: "css-loader",
          options: {
            importLoaders: 2,
          }
        }, 'postcss-loader', "less-loader"],
      },
      {
        test: /\.(styl|stylus)$/,
        use: ["style-loader", {
          loader: "css-loader",
          options: {
            importLoaders: 2,
          }
        }, 'postcss-loader', "stylus-loader"],
      },
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: ["babel-loader"],
      },
      {
        test: /\.vue$/,
        exclude: /(node_modules|bower_components)/,
        use: ["vue-loader"],
      },
      {
        test: /\.(png|jpg|gif|svg)$/i,
        type: "asset",
        generator: {
          filename: "img/[name].[hash:6][ext]",
        },
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024,
          },
        },
      },
      {
        test: /\.(ttf|eot|woff2?)$/i,
        type: "asset/resource",
        generator: {
          filename: "font/[name].[hash:6][ext]",
        },
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new htmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new VueLoaderPlugin(),
    new copyWebpackPlugin({
      patterns: [
        {
          from: "public",
          globOptions: {
            ignore: ["**/index.html"],
          },
          filter: async (resourcePath) => {
            if (resourcePath.includes("package.json")) {
              return false;
            }
            return true;
          },
        }
      ]
    }),
    new miniCssExtractPlugin({
      filename: 'css/[name].[hash:6].css'
    })
  ],
};
